package ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.peng.auctionsystem.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import base.BaseActivity;
import base.BaseFragment;
import butterknife.Bind;
import butterknife.ButterKnife;
import entity.CommondityListEntity;
import manager.ApiRequestManager;
import manager.RxSubscriber;
import ui.activity.ProductDetailActivity;
import ui.adapter.HomePageLotsAdapter;
import utils.ToastUtil;


/**
 * 首页
 */
public class HomepageFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private int page = 1;
    private int rows = 30;
    // 状态1表示拍卖中
    private String state = "1";


    @Bind(R.id.homepage_lv_all)
    ListView homepageLvAll;
    @Bind(R.id.homepage_iv_head)
    ImageView homepageIvHead;

    private HomePageLotsAdapter mAdapter;
    private List<CommondityListEntity.DataBean.RowsBean> commodityListData;
    private String commodityTitle;
    private String startTime;
    private String endTime;
    private String des;
    private int auctionGradient;
    private int currentPrice;
    private int commodityId;
    private int initialPrice;
    private String picPath;
    private Date date;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_homepage;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        homepageLvAll.setOnItemClickListener(this);
        homepageIvHead.setFocusable(true);
        homepageIvHead.setFocusableInTouchMode(true);
        homepageIvHead.requestFocus();
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();

        loadData();

    }

    private void loadData() {
        mRxManager.add(
                ApiRequestManager.getInstance().getCommodityList(page, rows, state)
                        .subscribe(new RxSubscriber<CommondityListEntity>((BaseActivity) getActivity()) {
                            @Override
                            protected void _onNext(CommondityListEntity baseResult) {
                                commodityListData = baseResult.getData().getRows();
                                mAdapter = new HomePageLotsAdapter(getActivity(), commodityListData);
                                homepageLvAll.setAdapter(mAdapter);
                            }

                            @Override
                            protected void _onError(String message) {
                                ToastUtil.showShort("登陆失败");
                            }
                        }));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        commodityTitle = commodityListData.get(position).getTitle();
        startTime = commodityListData.get(position).getStartTime();
        endTime = commodityListData.get(position).getEndTime();
        auctionGradient = commodityListData.get(position).getAuctionGradient();
        des = commodityListData.get(position).getRemark();
        currentPrice = commodityListData.get(position).getNowPrice();
        commodityId = commodityListData.get(position).getId();
        initialPrice = commodityListData.get(position).getStartPrice();
        picPath = commodityListData.get(position).getImagePath();

        Bundle data = new Bundle();
        data.putString("picPath",picPath);
        data.putInt("initialPrice",initialPrice);
        data.putString("commodityTitle", commodityTitle);

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            date = format1.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.putString("startTime",format2.format(date));
        try {
            date = format1.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        data.putString("endTime", format2.format(date));
        data.putInt("auctionGradient", auctionGradient);
        data.putString("des", des);
        data.putInt("currentPrice", currentPrice);
        data.putInt("commodityId", commodityId);

        Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
        intent.putExtra("data", data);
        intent.putExtras(data);
        startActivity(intent);
    }
}
